/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import org.opu.vcforms.editor.ButtonTabComponent.CloseActionListener;
import org.opu.vcforms.editor.actions.SaveEditAction;

/**
 * 
 * @author yura
 */
public class TabbedEditView extends JTabbedPane implements EditView {

    private static final long serialVersionUID = 6728506148610243782L;
    private PropertyChangeListener editChangeListener;
    
    private JFrame frame;
    private TitleI18n i18n;

    public TabbedEditView(PropertyChangeListener editChangeListener, 
            JFrame frame, TitleI18n i18n) {
        
        this.editChangeListener = editChangeListener;
        this.frame = frame;
        this.i18n = i18n;
    }

    public TabbedEditView(PropertyChangeListener editChangeListener,
            JFrame frame) {
        
        this(editChangeListener, frame, new TitleI18nImpl());
    }

    public TabbedEditView() {
        this(null, null);
    }

    @Override
    public TextView getSelectedText() {
        try {
            int selectedIndex = getSelectedIndex();
            TextView view = getView(selectedIndex);
            return view;
        } catch (Exception e) {
            throw new RuntimeException(e);
            // return null;
        }
    }

    @Override
    public JFrame getFrame() {
        return frame;
    }

    @Override
    public boolean isAllSaved() {
        try {
            for (int i = 0; i < getTabCount(); i++) {
                if (!getView(i).isSaved()) {
                    return false;
                }
            }
        } catch (Exception e) {
        }

        return true;
    }

    @Override
    public Iterator<TextView> iterator() {
        List<TextView> lst = new ArrayList<TextView>();
        try {
            for (int i = 0; i < getTabCount(); i++) {
                lst.add(getView(i));
            }
        } catch (Exception e) {
        }
        return lst.iterator();
    }

    public TextView getView(int index) {
        Component c = getComponentAt(index);// getTabComponentAt(index);

        if (c instanceof JScrollPane) {
            return (TextView) ((JScrollPane) c).getViewport().getComponent(0);
        }

        return (TextView) c;
    }

    @Override
    public void insertTab(String title, Icon icon, Component component, String tip, int index) {
        super.insertTab(title, icon, component, tip, index);
        super.setTabComponentAt(index, new ButtonTabComponent(this, new CloseActionListenerImpl()));
        setChangeList(component, this);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (editChangeListener != null) {
            editChangeListener.propertyChange(evt);
        }
    }

    @Override
    public void setChangeListener(PropertyChangeListener changeListener) {
        this.editChangeListener = changeListener;
        for (int i = 0; i < getTabCount(); i++) {
            getView(i).setChangeListener(this);
        }
    }

    @Override
    public PropertyChangeListener getChangeListener() {
        return editChangeListener;
    }

    @Override
    public void setSelectedIndex(int index) {
        int oldV = getSelectedIndex();
        super.setSelectedIndex(index);
        int newV = getSelectedIndex();
        propertyChange(new PropertyChangeEvent(this, CHANGE, oldV, newV));
    }

    private void setChangeList(Component component, PropertyChangeListener lst) {
        if (component instanceof JScrollPane) {
            Component c = ((JScrollPane) component).getViewport().getComponent(0);
            setChangeList(c, lst);
        } else {
            if (component instanceof TextView) {
                ((TextView) component).setChangeListener(this);
            } else {
                component.addPropertyChangeListener(this);
            }
        }
    }

    @Override
    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

    class CloseActionListenerImpl implements CloseActionListener {

        @Override
        public void actionPerformed(ActionEvent e, JTabbedPane pane, int i) {
            SaveEditAction sa = new SaveEditAction(TabbedEditView.this);
            try {
                TextView view = getView(i);

                if (!view.isSaved()) {
                    int ret = sa.optionMsg();
                    
                    switch (ret) {
                        case SaveEditAction.YES:
                            sa.action(view);
                            break;
                        case SaveEditAction.NO:
                            break;
                        case SaveEditAction.CANCEL:
                            return;
                    }
                }

                pane.remove(i);
            } catch (Exception ex) {
                sa.errorMsg(ex);
            }

            if (getTabCount() < 1){
                getFrame().setVisible(false);
            }
        }

        @Override
        public String getTooltipe() {
            return i18n.tabClose();
        }
    }
}
